package com.ruoyi.system.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.AppDynamicMsgMapper;
import com.ruoyi.system.service.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppDynamicLikeMapper;

/**
 * 点赞Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-27
 */
@Service
public class AppDynamicLikeServiceImpl extends ServiceImpl<AppDynamicLikeMapper, AppDynamicLike> implements IAppDynamicLikeService {
    @Autowired
    private AppDynamicLikeMapper appDynamicLikeMapper;
    @Autowired
    private IAppUserService iAppUserService;
    @Autowired
    private IAppDynamicService dynamicService;
    @Autowired
    private IAppUserRelationService iAppUserRelationService;
    @Autowired
    private IAppDynamicLikeService iAppDynamicLikeService;
    @Autowired
    private IAppDynamicCommentService iAppDynamicCommentService;
    @Autowired
    private IAppDynamicMsgService iAppDynamicMsgService;

    /**
     * 查询点赞
     *
     * @param id 点赞主键
     * @return 点赞
     */
    @Override
    public AppDynamicLike selectAppDynamicLikeById(Long id) {
        return appDynamicLikeMapper.selectAppDynamicLikeById(id);
    }

    /**
     * 查询点赞列表
     *
     * @param appDynamicLike 点赞
     * @return 点赞
     */
    @Override
    public List<AppDynamicLike> selectAppDynamicLikeList(AppDynamicLike appDynamicLike) {
        return appDynamicLikeMapper.selectAppDynamicLikeList(appDynamicLike);
    }

    /**
     * 新增点赞
     *
     * @param appDynamicLike 点赞
     * @return 结果
     */
    @Override
    public int insertAppDynamicLike(AppDynamicLike appDynamicLike) {
        return appDynamicLikeMapper.insertAppDynamicLike(appDynamicLike);
    }

    /**
     * 修改点赞
     *
     * @param appDynamicLike 点赞
     * @return 结果
     */
    @Override
    public int updateAppDynamicLike(AppDynamicLike appDynamicLike) {
        return appDynamicLikeMapper.updateAppDynamicLike(appDynamicLike);
    }

    /**
     * 批量删除点赞
     *
     * @param ids 需要删除的点赞主键
     * @return 结果
     */
    @Override
    public int deleteAppDynamicLikeByIds(Long[] ids) {
        return appDynamicLikeMapper.deleteAppDynamicLikeByIds(ids);
    }

    /**
     * 删除点赞信息
     *
     * @param id 点赞主键
     * @return 结果
     */
    @Override
    public int deleteAppDynamicLikeById(Long id) {
        return appDynamicLikeMapper.deleteAppDynamicLikeById(id);
    }

    @Override
    public int countByDynamicIdAndUserId(Long id, Long userId) {
        AppDynamicLike appDynamicLike = new AppDynamicLike();
        appDynamicLike.setId(id);
        appDynamicLike.setUserId(userId);
        List<AppDynamicLike> list = appDynamicLikeMapper.selectAppDynamicLikeList(appDynamicLike);
        return list.size();
    }

    @Override
    public AjaxResult addLikeDynamic(Long userId, Long dynamicId) {
        AppUser userEntity = iAppUserService.getById(userId);

        AppDynamicLike likeEntity = new AppDynamicLike();
        likeEntity.setCrtTime(new Date());
        likeEntity.setDynamicId(dynamicId);
        likeEntity.setUserId(userId);
        likeEntity.setUserName(userEntity.getUsername());
        likeEntity.setUserHeadImg(userEntity.getHeadImg());

        AppDynamic dynamicEntity = dynamicService.selectAppDynamicById(dynamicId);
        /*
         * 发送点赞通知消息
         * 对象：发送动态本人，点赞、评论的好友
         */
        List<AppDynamicMsg> msgList = Lists.newArrayList();
        String dynamicImg = "";
        // filterList内用户不再通知
        List<Long> filterList = Lists.newArrayList();
        filterList.add(userId);
        // 动态本人
        AppDynamicMsg msgEntity = new AppDynamicMsg();
        msgEntity.setCrtTime(likeEntity.getCrtTime());
        msgEntity.setUserId(dynamicEntity.getUserId());
        msgEntity.setDynamicId(dynamicId);
        msgEntity.setDynamicType(dynamicEntity.getType());
        msgEntity.setDynamicContent(dynamicEntity.getContent());
        if (dynamicEntity.getType() == 1) dynamicImg = JSONArray.parseArray(dynamicEntity.getImgs()).getString(0);
        else if (dynamicEntity.getType() == 2) dynamicImg = dynamicEntity.getVideoCover();
        msgEntity.setDynamicImg(dynamicImg);
        msgEntity.setMsgType(0);
        msgEntity.setFromUserId(userId);
        msgEntity.setFromUserName(userEntity.getUsername());
        msgEntity.setFromUserHeadImg(userEntity.getHeadImg());
        if (!filterList.contains(msgEntity.getUserId())) { // 动态本人点赞不通知
            filterList.add(msgEntity.getUserId());
            msgList.add(msgEntity);
        }

        /*
         * 获取好友idList
         */
        AppUserRelation appUserRelation = new AppUserRelation();
        appUserRelation.setUserId(userId);
        appUserRelation.setSelfState(0);
        appUserRelation.setSelfIsBeLooked(0);
        appUserRelation.setOtherState(0);
        appUserRelation.setOtherIsLook(0);
        // 动态的可见权限
        // 用户与好友设置的朋友圈权限
        List<Long> friendIdList = iAppUserRelationService.selectAppUserIds(appUserRelation);
        /*
         * 获取点赞列表，通知点赞好友
         */

        AppDynamicLike dynamicLike = new AppDynamicLike();
        dynamicLike.setDynamicId(dynamicEntity.getId());
        if (friendIdList.size() > 0) {
            dynamicLike.setUserIds(friendIdList);
        }

        List<AppDynamicLike> likeList = iAppDynamicLikeService.selectAppDynamicLikeList(dynamicLike);
        for (AppDynamicLike dynamicLikeEntity : likeList) {
            msgEntity = new AppDynamicMsg();
            msgEntity.setCrtTime(likeEntity.getCrtTime());
            msgEntity.setUserId(dynamicLikeEntity.getUserId());
            msgEntity.setDynamicId(dynamicId);
            msgEntity.setDynamicType(dynamicEntity.getType());
            msgEntity.setDynamicContent(dynamicEntity.getContent());
            msgEntity.setDynamicImg(dynamicImg);
            msgEntity.setMsgType(0);
            msgEntity.setFromUserId(userId);
            msgEntity.setFromUserName(userEntity.getUsername());
            msgEntity.setFromUserHeadImg(userEntity.getHeadImg());
            if (!filterList.contains(msgEntity.getUserId())) { // 已通知过得不通知
                filterList.add(msgEntity.getUserId());
                msgList.add(msgEntity);
            }
        }

        /*
         * 获取评论列表，通知评论好友
         */
        AppDynamicComment appDynamicComment = new AppDynamicComment();
        appDynamicComment.setDynamicId(dynamicEntity.getId());
        if (friendIdList.size() > 0) {
            appDynamicComment.setUserIds(friendIdList);
        }

        List<AppDynamicComment> commentList = iAppDynamicCommentService.selectAppDynamicCommentList2(appDynamicComment);

        for (AppDynamicComment commentEntity : commentList) {
            msgEntity = new AppDynamicMsg();
            msgEntity.setCrtTime(likeEntity.getCrtTime());
            msgEntity.setUserId(commentEntity.getUserId());
            msgEntity.setDynamicId(dynamicId);
            msgEntity.setDynamicType(dynamicEntity.getType());
            msgEntity.setDynamicContent(dynamicEntity.getContent());
            msgEntity.setDynamicImg(dynamicImg);
            msgEntity.setMsgType(0);
            msgEntity.setFromUserId(userId);
            msgEntity.setFromUserName(userEntity.getUsername());
            msgEntity.setFromUserHeadImg(userEntity.getHeadImg());

            if (!filterList.contains(msgEntity.getUserId())) { // 已通知过得不通知
                filterList.add(msgEntity.getUserId());
                iAppDynamicMsgService.insertAppDynamicMsg(msgEntity);
            }
        }

        // 保存点赞
        iAppDynamicLikeService.insertAppDynamicLike(likeEntity);

        // 动态增加点赞数
        dynamicService.addLikeCount(dynamicId);

        Map<String, Object> res = new HashMap<>();
        res.put("isAdd", true);
        return AjaxResult.ok(0, "", res);
    }

    @Override
    public AjaxResult cancelLikeDynamic(Long userId, Long id) {
        return null;
    }
}
